package com.sghd.football.robot.core;

import com.sghd.common.socket.client.Client;
import com.sghd.common.utils.math.RandomUtils;
import com.sghd.football.module.game.facade.model.GameResult;
import com.sghd.football.robot.action.Action;
import com.sghd.football.robot.action.ConnAction;
import com.sghd.football.robot.action.ErrorCode;
import com.sghd.football.robot.action.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author wch
 * @date 2018-07-06 12:15
 * @desc 机器人
 **/
public class Robot {
    private Logger logger = LoggerFactory.getLogger(Robot.class);
    /**id*/
    private long robotId;
    public String name;
    /***/
    private Action action;
    private Client client;
    private int state = ErrorCode.SUCCESS;
    private boolean complete = true;
    private GameResult gameResult;
    private boolean alive = false;
    private boolean close = false;
    private int times;
    //数据统计------------------------------------------------------------
    private boolean curBounds;
    private long money = 0;
    /** 旋转次数*/
    private long op = 0;
    /** 中免费的次数*/
    private long free = 0;
    /** 中小游戏的次数*/
    private long bounds = 0;
    /** 中奖次数*/
    private long rewards = 0;
    /** 普通时中大奖概率(中奖金额超过下注金额)*/
    private long bigWin = 0;

    /** 免费时的中奖次数*/
    private long freeWin;
    /** 免费总次数*/
    private long freeCount;
    /** 上一次下注的索引*/
    private int lastBet = -1;

    private Controller controller;

    public static Robot valueOf(long robotId, String name, Client client, Controller controller, int times) {
        Robot robot = new Robot();
        robot.robotId = robotId;
        robot.name = name;
        robot.client = client;
        robot.controller = controller;
        robot.action = new ConnAction();
        robot.times = times;
        return robot;
    }

    public void action() {
        if (canrun()) {
            complete = false;
            Stage stage = action.stage();
            controller.addWork(new Runnable() {
                @Override
                public void run() {
                    synchronized (Robot.this){
                        action.action(Robot.this);
                    }
                }
            }, RandomUtils.nextInt(stage.getDelay()));
        }
    }
    private boolean canrun() {
        return state == ErrorCode.SUCCESS && complete;
    }
    public void setNext(Action nextAction) {
        this.action = nextAction;
        state = ErrorCode.SUCCESS;
        complete = true;
        controller.addWork(new Runnable() {
            public void run() {
                action();
            }
        }, 0);
    }

    public long getRobotId() {
        return robotId;
    }

    public void setRobotId(long robotId) {
        this.robotId = robotId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Action getAction() {
        return action;
    }

    public void setAction(Action action) {
        this.action = action;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public Logger getLogger() {
        return logger;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public boolean isComplete() {
        return complete;
    }

    public void setComplete(boolean complete) {
        this.complete = complete;
    }

    public Controller getController() {
        return controller;
    }

    public void setController(Controller controller) {
        this.controller = controller;
    }

    public GameResult getGameResult() {
        return gameResult;
    }

    public void setGameResult(GameResult gameResult) {
        this.gameResult = gameResult;
    }

    public boolean isAlive() {
        return alive;
    }

    public void setAlive(boolean alive) {
        this.alive = alive;
    }

    public boolean isClose() {
        return close;
    }

    public void setClose(boolean close) {
        this.close = close;
    }

    public long getMoney() {
        return money;
    }

    public void setMoney(long money) {
        this.money = money;
    }

    public long getOp() {
        return op;
    }

    public void setOp(long op) {
        this.op = op;
    }

    public long getFree() {
        return free;
    }

    public void setFree(long free) {
        this.free = free;
    }

    public long getBounds() {
        return bounds;
    }

    public void setBounds(long bounds) {
        this.bounds = bounds;
    }

    public long getRewards() {
        return rewards;
    }

    public void setRewards(long rewards) {
        this.rewards = rewards;
    }

    public long getBigWin() {
        return bigWin;
    }

    public void setBigWin(long bigWin) {
        this.bigWin = bigWin;
    }

    public long getFreeWin() {
        return freeWin;
    }

    public void setFreeWin(long freeWin) {
        this.freeWin = freeWin;
    }

    public long getFreeCount() {
        return freeCount;
    }

    public void setFreeCount(long freeCount) {
        this.freeCount = freeCount;
    }

    public boolean isCurBounds() {
        return curBounds;
    }

    public void setCurBounds(boolean curBounds) {
        this.curBounds = curBounds;
    }

    public int getTimes() {
        return times;
    }

    public void setTimes(int times) {
        this.times = times;
    }

    public int getLastBet() {
        return lastBet;
    }

    public void setLastBet(int lastBet) {
        this.lastBet = lastBet;
    }
}
